package com.aikon.wht.json;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.*;

/**
 * Created by haitao.wang on 2016/11/29.
 */
public class TestOfJson {
    private static ObjectMapper objectMapper = new ObjectMapper();
    private static JsonFactory jsonFactory = new JsonFactory();

    public static <T> String writeValueAsJson(T t) throws IOException {

        Writer writer = new StringWriter(300);
        JsonGenerator jsonGenerator = jsonFactory.createGenerator(writer);
        objectMapper.writeValue(jsonGenerator,t);
        return writer.toString();
    }

    public static <T> T readValueFromJson(String json,Class<T> type) throws IOException {
        Reader reader = new StringReader(json);
        JsonParser jsonParser = jsonFactory.createParser(json);
        return objectMapper.readValue(jsonParser,type);
    }

    public static void main(String[] args) {
    }

}
